package com.liuxvru.project.user.controller;

import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.core.page.TableDataInfo;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.project.domain.Work;
import com.liuxvru.project.service.IWorkService;
import com.liuxvru.project.vo.WorkAllInitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/project/work")
public class UserWorkController extends BaseController {
    @Autowired
    private IWorkService workService;

    /**
     * 查询
     */
    @GetMapping("/list/{projectId}")
    public TableDataInfo list(@PathVariable("projectId") Long projectId, Work work) {
        startPage();
        work.setProjectId(projectId);
        List<Work> list = workService.selectWorkList(work);
        return this.getDataTable(list);
    }

    /**
     * 查询所有
     */
    @GetMapping("/listAll/{projectId}")
    public AjaxResult listAll(@PathVariable("projectId") Long projectId, Work work) {
        work.setProjectId(projectId);
        List<Work> list = workService.selectWorkList(work);
        return success(list);
    }

    /**
     * 查询我自己的
     */
    @GetMapping("/listMyAll")
    public AjaxResult listMyAll(Work work) {
        work.setAssignee(getUserId());
        List<Work> list = workService.selectWorkList(work);
        return success(list);
    }

    /**
     * 查询我自己的
     */
    @GetMapping("/workAllInit/{workItemId}")
    public AjaxResult workAllInit(@PathVariable("workItemId") Long workItemId) {
        WorkAllInitVo list = workService.workAllInit(workItemId);
        return success(list);
    }

    /**
     * 查询需求
     */
    @GetMapping("/listNeed/{projectId}")
    public TableDataInfo listNeed(@PathVariable("projectId") Long projectId, Work work) {
        startPage();
        List<Work> list = workService.listNeed(projectId, work);
        return this.getDataTable(list);
    }

    /**
     * 根据sprintId查询
     */
    @GetMapping("/getWorkBySprintId/{sprintId}")
    public AjaxResult getWorkBySprintId(@PathVariable("sprintId") Long sprintId) {
        List<Work> byId = workService.getWorkBySprintId(sprintId);
        return this.success(byId);
    }

    /**
     * 查询未归属于sprint的工作项
     */
    @GetMapping("/getWorkByNoSprintId/{projectId}")
    public AjaxResult getWorkByNoSprintId(@PathVariable("projectId") Long projectId) {
        List<Work> byId = workService.getWorkByNoSprintId(projectId);
        return this.success(byId);
    }

    @GetMapping("/getWorkById/{workId}")
    public AjaxResult getWorkById(@PathVariable("workId") Long workId) {
        return this.success(workService.getById(workId));
    }

    /**
     * 批量添加到sprint
     *
     * @param sprintId
     * @param workIds
     * @return
     */
    @Log(title = "批量添加到sprint", businessType = BusinessType.INSERT)
    @PostMapping("/workAddSprintByIds/{sprintId}")
    public AjaxResult workAddSprintByIds(@PathVariable("sprintId") Long sprintId, @RequestBody List<Long> workIds) {
        return this.success(workService.workAddSprintByIds(sprintId, workIds));
    }

    /**
     * 批量从sprint移除
     *
     * @param sprintId
     * @param workIds
     * @return
     */
    @Log(title = "批量从sprint移除", businessType = BusinessType.INSERT)
    @PostMapping("/workRemoveSprintByIds/{sprintId}")
    public AjaxResult workRemoveSprintByIds(@PathVariable("sprintId") Long sprintId, @RequestBody List<Long> workIds) {
        return this.success(workService.workRemoveSprintByIds(sprintId, workIds));
    }


    /**
     * 新增工作项
     */
    @PreAuthorize("@ss.hasPermi('project:work:add')")
    @Log(title = "工作项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Work work) {
        return toAjax(workService.add(work));
    }

    /**
     * 修改工作项
     */
    @Log(title = "工作项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Work work) {
        return toAjax(workService.updateWorkById(work));
    }

    /**
     * 删除工作项
     */
    @PreAuthorize("@ss.hasPermi('project:work:remove')")
    @Log(title = "工作项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workItemId}")
    public AjaxResult remove(@PathVariable Long workItemId) {
        return toAjax(workService.remove(workItemId));
    }
}
